﻿using System;
using System.Drawing;
using System.IO;
using System.Reflection;
using System.Windows.Forms;
using System.Text;

namespace AgilePokerCards
{
    public static class Helper
    {
        public static int _TotalButtonCount = -1;
        public const int MaxButtonsPerForm = 15;

        public static int TotalButtonCount
        {
            get
            {
                if (_TotalButtonCount == -1)
                {
                    int[] values = GetEnumValues(typeof(CardType));
                    _TotalButtonCount = values.Length;
                }

                return _TotalButtonCount;
            }
        }

        private static int[] GetEnumValues(Type type)
        {
            if(!type.IsEnum)
            {
                throw new ArgumentException("Type is not a Enum", "type");
            }

            FieldInfo[] fieldInfo = type.GetFields(BindingFlags.Static | BindingFlags.Public);
            int[] values = new int[fieldInfo.Length];

            for (int i = 0; i < fieldInfo.Length; i++)
            {
                values[i] = (int)fieldInfo[i].GetValue(null);
            }

            return values;
        }


        public static string GetCardDetails(CardType card)
        {
            string ret;

            switch (card)
            {
                case CardType.Zero:
                    ret = "0";
                    break;
                case CardType.Half:
                    ret = "½";
                    break;
                case CardType.One:
                    ret = "1";
                    break;
                case CardType.Two:
                    ret = "2";
                    break;
                case CardType.Three:
                    ret = "3";
                    break;
                case CardType.Five:
                    ret = "5";
                    break;
                case CardType.Eight:
                    ret = "8";
                    break;
                case CardType.Thirteen:
                    ret = "13";
                    break;
                case CardType.Twenty:
                    ret = "20";
                    break;
                case CardType.Forty:
                    ret = "40";
                    break;
                case CardType.Hunderd:
                    ret = "100";
                    break;
                case CardType.Infinity:
                    ret = "∞";
                    break;
                case CardType.Question:
                    ret = "?";
                    break;
                case CardType.More:
                    ret = "More";
                    break;
                case CardType.Back:
                    ret = "Back";
                    break;
                case CardType.Coffee:
                    ret = "Coffee";
                    break;
                case CardType.Smoke:
                    ret = "Smoke";
                    break;
                case CardType.ThumbsUp:
                    ret = "T-Up";
                    break;
                case CardType.ThumbsDown:
                    ret = "T-Down";
                    break;
                case CardType.Toilet:
                    ret = "Toilet";
                    break;
                default:
                    ret = "About";
                    break;
            }

            return ret;
        }

        public static string GetAboutMessage()
        {
            Assembly assembly = Assembly.GetExecutingAssembly();
            AssemblyName assemblyName = assembly.GetName();

            StringBuilder sb = new StringBuilder();
            sb.AppendLine("Agile Poker Cards v" + assemblyName.Version);
            sb.AppendLine("For");
            sb.AppendLine("Windows Mobile");
            sb.AppendLine("");
            sb.AppendLine("By");
            sb.AppendLine("Danny de Haas");
            sb.AppendLine("Amsterdam 2010");
            sb.AppendLine();
            sb.AppendLine("http://agilepokercards.codeplex.com/");

            return sb.ToString();            
        }

        public static void CreateButtons(Form form)
        {
            form.Controls.Clear();

            for (int index = 0; index < TotalButtonCount; index++)
            {
                CardType card = (CardType)index;

                Button button = new Button();
                button.Click += button_Click;

                button.BackColor = form.BackColor;
                button.ForeColor = form.ForeColor;

                button.Name = "card" + index;
                button.TabIndex = index;
                button.Tag = card;

                button.Visible = false;

                button.Text = GetCardDetails(card);

                form.Controls.Add(button);
            }
        }

        public static void PlaceButtons(FormStart form)
        {
            if(form.Controls.Count == 0)
            {
                return;
            }

            form.SuspendLayout();

            Size size = Screen.PrimaryScreen.WorkingArea.Size;

            int mx = 3;
            int my = 5;

            if (size.Width > size.Height)
            {
                mx = 5;
                my = 3;
            }

            int bw = size.Width / mx;
            int bh = size.Height / my;

            int start = form.StartIndex;
            int end = form.EndIndex;

            int position = 0;
            for (int index = 0; index < TotalButtonCount; index++)
            {
                Control button = form.Controls[index];

                if(index < start || index > end)
                {
                    button.Visible = false;
                    continue;
                }

                int x = position % mx;
                int y = position / mx;

                button.Location = new Point((x * bw), (y * bh));
                button.Size = new Size(bw, bh);
                button.Visible = true;

                position++;
            }

            form.ResumeLayout(false);
        }

        static void button_Click(object sender, EventArgs e)
        {
            Button button = (Button)sender;
            CardType card = (CardType)button.Tag;
            FormStart form = (FormStart) button.Parent;

            switch (card)
            {
                case CardType.About:
                    FormAbout formAbout = new FormAbout();
                    formAbout.Show();
                    break;

                case CardType.More:
                    form.ShowMore = true;
                    form.StartIndex = (int) CardType.Back;
                    form.EndIndex = TotalButtonCount;
                    break;

                case CardType.Back:
                    form.ShowMore = false;
                    form.StartIndex = 0;
                    form.EndIndex = MaxButtonsPerForm - 1;
                    break;

                default:
                    form.ShowMore = false;
                    form.StartIndex = 0;
                    form.EndIndex = MaxButtonsPerForm - 1;

                    FormCard formCard = new FormCard {Card = card};
                    formCard.Show();
                    break;
            }

            PlaceButtons(form);
        }

        public static Bitmap GetImageFromResources(CardType card)
        {
            string imageName = card + ".gif"; 

            string resourceName = string.Format("AgilePokerCards.Resources.{0}", imageName);
            using (Stream stream = Assembly.GetExecutingAssembly().GetManifestResourceStream(resourceName))
            {
                if (stream != null)
                {
                    return new Bitmap(stream);
                }

                return null;
            }
        }
    }
}
